//
//  ZTAirMonitorDevice.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/7.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "ZTAirMonitorDevice.h"
#import "XAIConvertorHelper.h"
#import "DataBaseManager.h"
@implementation ZTAirMonitorDevice
- (void)setCircuitOneStatus:(NSUInteger)statu
{
    [self setCircuitStatus:statu which:1];
}
- (void)setCircuitTwoStatus:(NSUInteger)statu
{
    [self setCircuitStatus:statu which:2];
}
- (void)setCircuitThreeStatus:(NSUInteger)statu
{
    [self setCircuitStatus:statu which:3];
}
- (void)setCircuitFourStatus:(NSUInteger)statu
{
    [self setCircuitStatus:statu which:4];
}

- (void)setCircuitFiveStatus:(NSUInteger)statu
{
    [self setCircuitStatus:statu which:5];
}


- (void)reciveStatus:(uint32_t)gatewayApsn
          deviceLuid:(uint64_t)deviceLuid
         triggerApsn:(uint32_t)triggerApsn
         triggerLuid:(uint64_t)triggerLuid
           timestamp:(uint32_t)timestamp
            statusID:(uint8_t)statusID
                data:(XAITLV *)data
           isHistory:(BOOL)isHistory
{
    if (!data) {
        NSLog(@"reciveStatus 收到的数据为空");
    
        return;
    }
    [super reciveStatus:gatewayApsn
             deviceLuid:deviceLuid
            triggerApsn:triggerApsn
            triggerLuid:triggerLuid
              timestamp:timestamp
               statusID:statusID
                   data:data
              isHistory:isHistory];
    switch (statusID)
    {
        
        case 0x01: //PM2.5
        {
            uint16_t status = 0;
            if (data.payloadlen != sizeof(uint16_t) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], sizeof(uint16_t));
            status = CFSwapInt16(status);
            if (isHistory && !self.isSendAirDataEnable) {
                [kDataBaseManager insertAirWithApsn:gatewayApsn luid:deviceLuid time:timestamp type:0x01 value:status];
            }
            else
            {
                self.pmValue = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
        }
            break;
        //甲醛 0d
        case 0x02:
        {
            uint16_t status = 0;
            if (data.payloadlen != sizeof(uint16_t) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], sizeof(uint16_t));
            status = CFSwapInt16(status);
            if (isHistory && !self.isSendAirDataEnable) {
                 [kDataBaseManager insertAirWithApsn:gatewayApsn luid:deviceLuid time:timestamp type:0x02 value:status];
            }
            else
            {
                NSLog(@"收到甲醛");
                self.formaldehydeValue = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
            
//            NSString *str = nil;
//            if (status == ZTDeviceChannelStatusOn)
//            {
//                str = [NSString stringWithFormat:@"%@二路打开",[self getDeviceName]];
//            }
//            else
//            {
//                str = [NSString stringWithFormat:@"%@二路关闭",[self getDeviceName]];
//            }
//            [self updateOperationWithTriggerApsn:triggerApsn triggerLuid:triggerLuid timestamp:timestamp operation:str];
        }
            break;
        //温度 0c
        case 0x03:
        {
            int16_t status = 0;
            if (data.payloadlen != sizeof(int16_t) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], data.payloadlen);
            status = CFSwapInt16(status);
            if (isHistory && !self.isSendAirDataEnable) {
                 [kDataBaseManager insertAirWithApsn:gatewayApsn luid:deviceLuid time:timestamp type:0x03 value:status];
            }
            else
            {
                self.tempValue = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
            
//            NSString *str = nil;
//            if (status == ZTDeviceChannelStatusOn)
//            {
//                str = [NSString stringWithFormat:@"%@三路打开",[self getDeviceName]];
//            }
//            else
//            {
//                str = [NSString stringWithFormat:@"%@三路关闭",[self getDeviceName]];
//            }
//            [self updateOperationWithTriggerApsn:triggerApsn triggerLuid:triggerLuid timestamp:timestamp operation:str];
        }
            break;
        //湿度 06 XAI_DATA_TYPE_BIN_PERCENT
        case 0x04:
        {
            uint8_t status = 0;
            if (data.payloadlen != sizeof(uint8_t) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], sizeof(uint8_t));
            if (isHistory && !self.isSendAirDataEnable) {
                 [kDataBaseManager insertAirWithApsn:gatewayApsn luid:deviceLuid time:timestamp type:0x04 value:status];
            }
            else
            {
                self.humidityValue = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
            
//            NSString *str = nil;
//            if (status == ZTDeviceChannelStatusOn)
//            {
//                str = [NSString stringWithFormat:@"%@四路打开",[self getDeviceName]];
//            }
//            else
//            {
//                str = [NSString stringWithFormat:@"%@四路关闭",[self getDeviceName]];
//            }
//            [self updateOperationWithTriggerApsn:triggerApsn triggerLuid:triggerLuid timestamp:timestamp operation:str];
        }
            break;
        //光照 0d
        case 0x05:
        {
            uint16_t status = 0;
            if (data.payloadlen != sizeof(uint16_t) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], sizeof(uint16_t));
            status = CFSwapInt16(status);
            if (isHistory && !self.isSendAirDataEnable) {
                 [kDataBaseManager insertAirWithApsn:gatewayApsn luid:deviceLuid time:timestamp type:0x05 value:status];
            }
            else
            {
                self.illuminationValue = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
            
//            NSString *str = nil;
//            if (status == ZTDeviceChannelStatusOn)
//            {
//                str = [NSString stringWithFormat:@"%@四路打开",[self getDeviceName]];
//            }
//            else
//            {
//                str = [NSString stringWithFormat:@"%@四路关闭",[self getDeviceName]];
//            }
//            [self updateOperationWithTriggerApsn:triggerApsn triggerLuid:triggerLuid timestamp:timestamp operation:str];
        }
            break;
            
        case 0x06:
        {
            BOOL status = 0;
            if (data.payloadlen != sizeof(BOOL) ) {
                return;
            }
            memcpy(&status, [data getPayloadbyte], sizeof(BOOL));
            if (isHistory) {
                
                
            }
            else
            {
                self.isSendAirDataEnable = status;
                [kDataManager deviceRefreshCallBackWithType:DeviceCallbackTypeStatus];
            }
        }
            break;

        default:
            break;
    }
}

@end
